package mws;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
public class ClientContext {
    private final static Logger log = LoggerFactory.getLogger(ClientContext.class);

    @Getter
    @Setter
    private List<MappingInterface> pipline;
    // 这是 并行责任链
    // 串联责任链 用的就是 类的一个上下文 接口
    public final MapContext mapContext;

    public ClientContext init(){
        pipline = new ArrayList<>();
        return this;
    }

    public ClientContext add(MappingInterface mappingInterface) {
        pipline.add(mappingInterface);
        return this;
    }

    public ClientContext add(List<MappingInterface> mappingInterface) {
        pipline.addAll(mappingInterface);
        return this;
    }
    public void  process(String inputLine) throws Exception {
        if(mapContext == null) {
            log.error("mapContext is null");
            throw  new Exception("mapContext is null");
        }

        if (mapContext.isStopped()) {
            log.error("mapContext is stopped");
            throw  new Exception("mapContext is stopped");
        }

        for (MappingInterface mappingInterface : pipline) {
            mappingInterface.mapping(inputLine, mapContext);
        }
    }


}
